package com.business.lgh.nacosclient;

import com.business.lgh.nacos.api.common.PropertyKeyConst;
import com.business.lgh.nacos.api.naming.pojo.Instance;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author lgh
 * @version V1.0
 * @Package com.business.lgh.nacosclient
 * @date 2020/6/25 5:29 下午
 */
public class NacosListener implements ApplicationListener<WebServerInitializedEvent> {

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {

        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, "192.168.0.107:8848");
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        properties.put(PropertyKeyConst.PASSWORD, "nacos");

        NamingService namingService = new NacosNamingService(properties);

        Instance instance = new Instance();
        instance.setIp("192.168.0.107");
        instance.setPort(8019);
        instance.setServiceName("DEFAULT_GROUP@@service-provider");
        instance.setWeight(2);
        instance.setEphemeral(true);
        Map<String, String> map = new HashMap<String, String>();
        map.put("netType", "external");
        map.put("version", "2.0");

        namingService.registerInstance("nacos.test.1", instance);

    }
}
